using System;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;

namespace MvvmBase.ViewModel
{
    public class InternalNavigator : INavigator
    {
        private readonly PhoneApplicationFrame _navigationService;

        public InternalNavigator(PhoneApplicationFrame navigationService)
        {
            _navigationService = navigationService;
            _navigationService.Navigating += (sender, args) =>
                {
                    switch (args.NavigationMode)
                    {
                        case NavigationMode.Back:
                            {
                                var eventArgs = new NavigatingFromEventArgs();

                                OnNavigatingBack(eventArgs);
                                args.Cancel = eventArgs.Cancel;
                            }
                            break;
                        default:
                            {
                                var eventArgs = new NavigatingFromEventArgs();
                                OnNavigatingFrom(eventArgs);
                                args.Cancel = eventArgs.Cancel;
                            }
                            break;
                    }
                };
            _navigationService.Navigated += (sender, args) =>
                {
                    if (args.NavigationMode == NavigationMode.Back)
                    {
                        OnNavigatedBack(EventArgs.Empty);
                    }
                };
        }

        #region INavigator Members

        public event EventHandler<NavigatingFromEventArgs> NavigatingFrom;

        public event EventHandler<NavigatingFromEventArgs> NavigatingBack;

        public event EventHandler NavigatedBack;

        public void Navigate(NavigationPlace navigationPlace, params object[] pars)
        {
            _navigationService.StopLoading();
            switch (navigationPlace)
            {
                case NavigationPlace.Home:
                    _navigationService.Navigate(new Uri(@"/Views\HomeView.xaml", UriKind.RelativeOrAbsolute));
                    break;
                default:
                    throw new NotImplementedException();
            }
        }

        public void GoBack()
        {
            _navigationService.GoBack();
        }

        #endregion

        public void OnNavigatingFrom(NavigatingFromEventArgs e)
        {
            EventHandler<NavigatingFromEventArgs> handler = NavigatingFrom;
            if (handler != null) handler(this, e);
        }

        public void OnNavigatingBack(NavigatingFromEventArgs e)
        {
            EventHandler<NavigatingFromEventArgs> handler = NavigatingBack;
            if (handler != null) handler(this, e);
        }

        public void OnNavigatedBack(EventArgs e)
        {
            EventHandler handler = NavigatedBack;
            if (handler != null) handler(this, e);
        }
    }
}